<template>
  <el-dialog
      :title="tagID?'修改标签':'新增标签'"
      :visible="dialogVisible"
      width="20%"
      @close="closeTagsDialog"
      @open="openTagsDialog"
    >
      <el-form
        :model="tagForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
          <el-select v-model="tagForm.subjectID" placeholder="请选择" style="width:100%" v-if="!$route.query.id">
            <el-option
              v-for="item in subjectsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            v-model="tagForm.tagName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input')">取 消</el-button>
        <el-button type="primary" @click="submit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, detail, update } from '@/api/hmmm/tags.js'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    tagID: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      tagForm: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'change' }
        ]
      },
      subjectsList: []
    }
  },
  methods: {
    closeTagsDialog () {
      this.$emit('input')
      this.$refs.ruleForm.resetFields()
    },
    async openTagsDialog () {
      const res = await simple()
      console.log('获取学科列表', res)
      this.subjectsList = res.data
    },
    submit () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) { return }
        if (this.tagID) {
          await update({ ...this.tagForm, id: this.tagID })
          this.$emit('search-list')
        } else {
          if (this.$route.query.id) {
            await add({
              subjectID: Number(this.$route.query.id),
              tagName: this.tagForm.tagName
            })
          } else {
            await add(this.tagForm)
          }
          this.$emit('paramsReset')
          this.$emit('get-taglist')
        }
        const msg = this.tagID ? '修改标签成功' : '新增标签成功'
        this.$message.success(msg)
        this.$emit('input')
      })
    },
    async editTag (id) {
      const res = await detail({ id: id })
      // console.log(res)
      this.tagForm = res.data
      console.log(res)
    }
  }
}
</script>

<style scoped lang='less'></style>
